# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
 -------------------------------------------------
    File Name:     cli_manager.py
    Description:   命令入口文件
 -------------------------------------------------
 """

import json
import subprocess
import argparse
from pathlib import Path


class CLIManager:
    """Command Line Interface (CLI) manager class."""

    # 命令配置文件
    CONFIG_FILE = Path(__file__).parent / "commands.json"

    @staticmethod
    def load_commands():
        """Load commands from configuration file."""
        with open(CLIManager.CONFIG_FILE, "r") as f:
            return json.load(f)

    @staticmethod
    def execute_command(command):
        """Execute the shell command."""
        try:
            subprocess.run(command, shell=True, check=True)
        except subprocess.CalledProcessError as e:
            print(f"Error executing command: {e}")
            raise

    @staticmethod
    def show_help(commands, lang):
        print("Available commands:")
        for command, details in commands.items():
            description = details['description'].get(lang, details['description']['en'])
            print(f"  {command:30} - {description}")


def main():
    commands = CLIManager.load_commands()

    parser = argparse.ArgumentParser(description="API CLI tool")
    parser.add_argument("command", help="Command to execute")
    parser.add_argument("--lang", choices=['en', 'zh'], default='zh', help="Language for help text")
    args = parser.parse_args()

    if args.command in commands:
        cmd = commands[args.command]["command"]
        CLIManager.execute_command(cmd)
    else:
        print(f"Unknown command: {args.command}")
        CLIManager.show_help(commands, args.lang)


if __name__ == "__main__":
    main()
